package com.zrkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrkj.entity.TSysResource;
import com.zrkj.entity.TSysRole;
import com.zrkj.entity.TSysRoleResource;
import com.zrkj.entity.TSysRoleStation;
import com.zrkj.entity.vo.Result;
import com.zrkj.mapper.TSysRoleMapper;
import com.zrkj.service.ITSysResourceService;
import com.zrkj.service.ITSysRoleResourceService;
import com.zrkj.service.ITSysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 国良
 * @className : ITSysRoleServiceImpl
 * @package: com.ha.service.impl
 * @description : 用户组表 服务接口实现
 * @date :  2021-05-14
 */
@Service("iTSysRoleService")
public class ITSysRoleServiceImpl extends ServiceImpl<TSysRoleMapper, TSysRole> implements ITSysRoleService {
    @Autowired
    private ITSysRoleResourceService itSysRoleResourceService;


    /**
     * 添加
     *
     * @param tSysRole
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> add(TSysRole tSysRole) {
        return Result.success(this.save(tSysRole));
    }

   @Transactional
    public Result roleResourceAndStation(TSysRole tSysRole){
       String uuid = IdWorker.get32UUID();
       tSysRole.setId(uuid);
       this.save(tSysRole);
        List<String> resources = tSysRole.getResources();
        for (String resource : resources) {
            TSysRoleResource roleResource = new TSysRoleResource();
            roleResource.setRoleId(tSysRole.getId());
            roleResource.setResourceId(resource);
            itSysRoleResourceService.add(roleResource);
        }

        return Result.success();
    }

    /**
     * 更新
     *
     * @param tSysRole
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> update(TSysRole tSysRole) {
        //先删除
        String id = tSysRole.getId();
        this.removeById(id);
        TSysRoleResource sysRoleResource = new TSysRoleResource();
        sysRoleResource.setRoleId(id);
        itSysRoleResourceService.deletebyCon(sysRoleResource);
        TSysRoleStation station = new TSysRoleStation();
        station.setRoleId(id);

        //再加上去
        tSysRole.setId(id);
        this.save(tSysRole);
        List<String> resources = tSysRole.getResources();
        for (String resource : resources) {
            TSysRoleResource roleResource = new TSysRoleResource();
            roleResource.setRoleId(tSysRole.getId());
            roleResource.setResourceId(resource);
            itSysRoleResourceService.add(roleResource);
        }


        return Result.success();
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> delete(List<String> idList) {
        TSysRoleResource resource = new TSysRoleResource();
        resource.setRoleIds(idList);
        itSysRoleResourceService.deletebyCon(resource);
        TSysRoleStation station = new TSysRoleStation();
        station.setRoleIds(idList);
        return Result.success(this.removeByIds(idList));
    }

    @Autowired
    private ITSysResourceService itSysResourceService;
    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public Result<TSysRole> getById(String id) {
        TSysRole role = baseMapper.selectById(id);
        TSysRoleResource resource = new TSysRoleResource();
        resource.setRoleId(id);
        Result<List<TSysRoleResource>> result = itSysRoleResourceService.queryList(resource);
        if(!CollectionUtils.isEmpty(result.getData())){
            List<String> collect = result.getData().stream().map(i -> i.getResourceId()).collect(Collectors.toList());
            TSysResource tSysResource = new TSysResource();
            Result<List<TSysResource>> listResult = itSysResourceService.queryList(tSysResource.setIds(collect));
            role.getResourcesInfo().addAll(listResult.getData());
        }

        return Result.success(role);
    }
    /**
     * 查询列表
     *
     * @param tSysRole
     * @return
     */
    @Override
    public Result<List<TSysRole>> queryList(TSysRole tSysRole) {
        QueryWrapper<TSysRole> queryWrapper = buildQuery(tSysRole);
        return Result.success(this.list(queryWrapper));
    }

    public Result<IPage<TSysRole>> queryPageList(TSysRole data) {
        QueryWrapper<TSysRole> queryWrapper = buildQuery(data);
        IPage<TSysRole> pageQuery = this.page(data.getPage(), queryWrapper);
        return Result.success(pageQuery);
    }

    private QueryWrapper<TSysRole> buildQuery(TSysRole data){
        QueryWrapper<TSysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(data.getId()),"`id`",data.getId());
        queryWrapper.eq(StringUtils.isNotBlank(data.getName()),"`name`",data.getName());
        return queryWrapper;
    }
}
